﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using System.Configuration;
using System.IO;
using System.Reflection;

using EnvironmentalMonitor.Support.Device;
using EnvironmentalMonitor.Support.Device.Handler;
using EnvironmentalMonitor.Support.Module.Manage;
using EnvironmentalMonitor.Support.Module;
using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.WebApplication
{
    public class Global : System.Web.HttpApplication
    {
        public delegate int CompareType(Type x, Type y);

        protected void Application_Start(object sender, EventArgs e)
        {
            #region 参数配置
            string configurationFile = string.Format("{0}Parameter.config", HttpRuntime.AppDomainAppPath);
            Variable.Initialize(configurationFile);
            #endregion

            #region 日志目录
            DirectoryInfo logDirectory = Variable.Logger.Root;
            if (logDirectory != null)
            {
                Variable.LogDirectory = logDirectory.FullName;
            }
            #endregion

            #region 网络监听
            Listener.InstructionHandlers = new List<InstructionHandler>();

            LogHandler logHandler = new LogHandler();
            Listener.InstructionHandlers.Add(logHandler);

            InstanceHandler instanceHandler = new InstanceHandler();
            Listener.InstructionHandlers.Add(instanceHandler);

            if (!Hardware.FatalError)
            {
                Listener.Run();
            }
            #endregion

            #region 菜单和页面
            List<Menu> menus = new List<Menu>();
            List<Page> pages = new List<Page>();

            Type type = typeof(EnvironmentalMonitor.WebApplication.Abstract.AbstractPage);
            Assembly assembly = Assembly.GetAssembly(type);
            Type[] types = assembly.GetTypes();

            Array.Sort(types, delegate(Type x, Type y)
            {
                return string.Compare(x.FullName, y.FullName);
            });

            for (int i = 0; i < types.Length; i++)
            {
                if ((types[i].IsSubclassOf(typeof(System.Web.UI.Page))) && (!types[i].IsAbstract))
                {
                    Menu menu = new Menu();

                    int rootLength = typeof(EnvironmentalMonitor.WebApplication.Global).Namespace.Length + 1;
                    menu.Name = types[i].Name;
                    menu.CatalogId = types[i].FullName.Substring(0, types[i].FullName.Length - types[i].Name.Length);
                    menu.CatalogId = menu.CatalogId.Substring(rootLength).Replace(".", "/");
                    if (string.IsNullOrEmpty(menu.CatalogId))
                    {
                        menu.CatalogId = "/";
                    }
                    menu.CatalogName = menu.CatalogId;
                    menu.Url = string.Format("{0}{1}.aspx", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath, types[i].FullName.Substring(rootLength).Replace(".", "/"));
                    menus.Add(menu);
                }
                if ((types[i].IsSubclassOf(typeof(EnvironmentalMonitor.WebApplication.Abstract.AbstractSecurityPage))) && (!types[i].IsAbstract))
                {
                    Page page = new Page();
                    page.Name = types[i].FullName;
                    pages.Add(page);
                }
            }

            this.Application.Add(Constant.APPLICATION_KEY_MENUS, menus);
            this.Application.Add(Constant.APPLICATION_KEY_PAGES, pages);
            #endregion
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            EnvironmentalMonitor.Support.Module.Manage.User user = new EnvironmentalMonitor.Support.Module.Manage.User();
            user.Prerogative = false;
            this.Session.Add(Constant.SESSION_KEY_USER, user);
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            Listener.Shutdown();
        }
    }
}